package com.fpost.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fpost.constants.ActionConstants;
import com.fpost.domain.Article;
import com.fpost.form.dto.ArticleDTO;
import com.fpost.manager.ArticleManager;

public class ArticleUtils {
	
	public static List<ArticleDTO> getMostRecentCurrentArticles(int maxResults) throws Exception {
		
		List<Article> recentArticles = new ArticleManager().getMostRecentCurrentArticles(maxResults);
		List<ArticleDTO> articleDTOList = new ArrayList<ArticleDTO>();
		for (Article article : recentArticles) {
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setSnapshotProperties(article);
			articleDTOList.add(articleDTO);
		}		
		return articleDTOList;
	}
	
	public static List<ArticleDTO> getMostRecentCurrentArticles(Date from, Date to, int maxResults) throws Exception {
		
		List<Article> recentArticles = new ArticleManager().getMostRecentCurrentArticles(from, to, maxResults);
		List<ArticleDTO> articleDTOList = new ArrayList<ArticleDTO>();
		for (Article article : recentArticles) {
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setSnapshotProperties(article);
			articleDTOList.add(articleDTO);
		}		
		return articleDTOList;
	}
	
	public static List<ArticleDTO> getMostPopularCurrentArticles(int maxResults) throws Exception {

		List<Article> popularArticles = new ArticleManager().getMostPopularCurrentArticles(maxResults);
		List<ArticleDTO> articleDTOList = new ArrayList<ArticleDTO>();
		for (Article article : popularArticles) {
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setSnapshotProperties(article);
			articleDTOList.add(articleDTO);
		}		
		return articleDTOList;
	}
	
	public static List<ArticleDTO> getMostPopularCurrentArticles(Date from, Date to, int maxResults) throws Exception {

		List<Article> popularArticles = new ArticleManager().getMostPopularCurrentArticles(from, to, maxResults);
		List<ArticleDTO> articleDTOList = new ArrayList<ArticleDTO>();
		for (Article article : popularArticles) {
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setSnapshotProperties(article);
			articleDTOList.add(articleDTO);
		}		
		return articleDTOList;
	}	
	
	public static List<ArticleDTO> getHighestRatingCurrentArticles(java.sql.Date from, java.sql.Date to, int maxResults) throws Exception {

		List<Object[]> objectArrays = new ArticleManager().getHighestRatingCurrentArticles_SQL(from, to, maxResults);
		List<ArticleDTO> articleDTOList = new ArrayList<ArticleDTO>();
		for (int i = 0; i < (objectArrays.size() < maxResults ? objectArrays.size() : maxResults); i++) {			
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setPropertiesForHighestRating(objectArrays.get(i));
			articleDTOList.add(articleDTO);			
		}		
		return articleDTOList;
	}	
}
